package com.xhwl.logistics.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.vehicle.SyncTypeEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.query.cent.device.ProductAccessQuery;
import com.xhwl.common.vo.cent.vehicle.SyncVO;
import com.xhwl.logistics.service.IBasicInfoSyncService;
import com.xhwl.logistics.service.cent.IProductAccessCentService;
import com.xhwl.logistics.strategy.BasicInfoSyncStrategy;
import com.xhwl.logistics.strategy.context.BasicInfoSyncStrategyContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description:
 * @author: lu.Aaron
 * @create: 2021-06-29 15:53
 **/
//@Service
public class BasicInfoSyncServiceImpl implements IBasicInfoSyncService {


    @Autowired
    private BasicInfoSyncStrategyContext basicInfoSyncStrategyContext;
    @Autowired
    private IProductAccessCentService productAccessCentService;

    @Override
    public ResultJson syncByType(Short industryId, SyncVO syncVO) {
        //根据项目id查询第三方车场id
        @NotNull Integer projectId = syncVO.getProjectId();
        List<DeviceProjSupplierCategory> deviceProjSupplierCategories = productAccessCentService.listProductAccess(industryId, new ProductAccessQuery(projectId, CategoryEnum.YARD_GATE.getId()));
        if (deviceProjSupplierCategories.isEmpty()) {
            throw new BusinessException("该项目未配置供应商，请先配置");
        }
        DeviceProjSupplierCategory productAccess = deviceProjSupplierCategories.get(0);
        JSONObject properties = productAccess.getProperties();
        if (null==properties) {
            throw new BusinessException("该项目未配置第三方车场id，请先在供应商管理平台配置");
        }
        String thirdParkId = (String) properties.get("thirdParkId");
        syncVO.setParkId(thirdParkId);
        @NotBlank SyncTypeEnum type = syncVO.getType();
        if (type.equals(SyncTypeEnum.ALL)) {
            //分别调用车场 区域 通道 设备同步，一次同步完
            BasicInfoSyncStrategy parkingLotBasicInfoSyncService = basicInfoSyncStrategyContext.getBasicInfoSyncService(SyncTypeEnum.PARKINGLOT);
            parkingLotBasicInfoSyncService.basicInfoSync(industryId, syncVO);
            BasicInfoSyncStrategy areaBasicInfoSyncService = basicInfoSyncStrategyContext.getBasicInfoSyncService(SyncTypeEnum.AREA);
            areaBasicInfoSyncService.basicInfoSync(industryId, syncVO);
            BasicInfoSyncStrategy passageBasicInfoSyncService = basicInfoSyncStrategyContext.getBasicInfoSyncService(SyncTypeEnum.PASSAGE);
            passageBasicInfoSyncService.basicInfoSync(industryId, syncVO);
            BasicInfoSyncStrategy deviceBasicInfoSyncService = basicInfoSyncStrategyContext.getBasicInfoSyncService(SyncTypeEnum.DEVICE);
            deviceBasicInfoSyncService.basicInfoSync(industryId, syncVO);
            return ResultJson.success("同步车场基础数据成功");
        }else {
            //根据type以及策略模式获取对应的接口实现类（车场、区域、通道、设备），可分步同步
            BasicInfoSyncStrategy basicInfoSyncService = basicInfoSyncStrategyContext.getBasicInfoSyncService(type);
            ResultJson resultJson = basicInfoSyncService.basicInfoSync(industryId, syncVO);
            return resultJson;
        }

    }
}

